package com.punai.dataanalysis.epdatakb.service.NG001change1212;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgccbqkhzb;
import com.punai.dataanalysis.epdatakb.mapper.NG001change1212.YearGcQkC1212Mapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.bo.RgJxD;
import com.punai.dataanalysis.ng001.bo.YearLjWccz;
import com.punai.dataanalysis.ng001.domain.ProjectTable;
import com.punai.prodatalibrary.domain.bo.GlobalProInfoBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class NG001NdgccbqkhzbC1212Service {

    @Autowired
    private YearGcQkC1212Mapper yearGcQkC1212Mapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {

        Map<String,Object> params = new HashMap<>();
        params.put("fgs",dataAnalysisVo.getFgs());
        params.put("month1",dataAnalysisVo.isMonth1());

        List<DataAnalysisNdgccbqkhzb> resList = yearGcQkC1212Mapper.getInfos(params);
        List<DataAnalysisNdgccbqkhzb> resList2 = yearGcQkC1212Mapper.getInfos2(params);


        Map<String, DataAnalysisNdgccbqkhzb> info2Map = resList2.stream().filter(e -> StrUtil.isNotBlank(e.getPc())).collect(Collectors.toMap(DataAnalysisNdgccbqkhzb::getPc, Function.identity(), (v1, v2) -> v2));


        List<DataAnalysisNdgccbqkhzb> res = new ArrayList<>();
        for (DataAnalysisNdgccbqkhzb qk : resList) {
            String pc = qk.getPc();
            if (StrUtil.isEmpty(pc)) {
                continue;
            }
            DataAnalysisNdgccbqkhzb qk2 = info2Map.getOrDefault(pc,new DataAnalysisNdgccbqkhzb());
            qk.setYearCostRgfJe(qk2.getYearCostRgfJe());
            qk.setYearCostClfJe(qk2.getYearCostClfJe());
            qk.setYearCostJxfJe(qk2.getYearCostJxfJe());
            qk.setYearCostQtzjfJe(qk2.getYearCostQtzjfJe());
            qk.setYearCostJjfJe(qk2.getYearCostJjfJe());
            qk.setYearCostJjfGlrygzJe(qk2.getYearCostJjfGlrygzJe());
            qk.setYearCostFbgczcJe(qk2.getYearCostFbgczcJe());
            qk.setYearCostFyglJe(qk2.getYearCostFyglJe());
            qk.setYearCostCwfyJe(qk2.getYearCostCwfyJe());
            qk.setYearCostQtJe(qk2.getYearCostQtJe());
            calcZb(qk);
            res.add(qk);
        }
        // 目前非全字段 有些字段未表明取值 或无法取值 且类型为String(未修改)
        // 顺序 不要变
        DataAnalysisNdgccbqkhzb zj = zj(res);
        zj.setProName("总计");
        res = xmbFz(res);
        zyfz(res);
        res.add(zj);
        return res;
    }

    private List<DataAnalysisNdgccbqkhzb> xmbFz(List<DataAnalysisNdgccbqkhzb> res) {
        List<DataAnalysisNdgccbqkhzb> newResList = new ArrayList<>();
        Map<String, List<DataAnalysisNdgccbqkhzb>> xmbGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getDeptName())).collect(Collectors.groupingBy(DataAnalysisNdgccbqkhzb::getDeptName));
        for (Map.Entry<String, List<DataAnalysisNdgccbqkhzb>> entry : xmbGroup.entrySet()) {
            String key = entry.getKey();
            List<DataAnalysisNdgccbqkhzb> value = entry.getValue();
            newResList.addAll(value);
            DataAnalysisNdgccbqkhzb zj = zj(value);
            zj.setProName(key  + "小计");
            newResList.add(zj);
        }
        return newResList;
    }

    private void calcZb(DataAnalysisNdgccbqkhzb qk) {
        // 产值盘点
        BigDecimal yearCzPdHj = qk.getYearCzPdHj();
        // 其中专业分包
        BigDecimal yearCzPdZyfb = qk.getYearCzPdZyfb();
        // 甲供材
        BigDecimal yearCzJgc = qk.getYearCzJgc();
        // 对比值
        BigDecimal basePrice = NumberUtil.add(NumberUtil.subtract(yearCzPdHj, yearCzPdZyfb), yearCzJgc);


        BigDecimal yearCostRgfJe        = qk.getYearCostRgfJe();
        BigDecimal yearCostClfJe        = qk.getYearCostClfJe();
        BigDecimal yearCostJxfJe        = qk.getYearCostJxfJe();
        BigDecimal yearCostQtzjfJe      = qk.getYearCostQtzjfJe();
        BigDecimal yearCostJjfJe        = qk.getYearCostJjfJe();
        BigDecimal yearCostJjfGlrygzJe  = qk.getYearCostJjfGlrygzJe();
        BigDecimal yearCostFbgczcJe     = qk.getYearCostFbgczcJe();
        BigDecimal yearCostFyglJe       = qk.getYearCostFyglJe();
        BigDecimal yearCostCwfyJe       = qk.getYearCostCwfyJe();
        BigDecimal yearCostQtJe         = qk.getYearCostQtJe();

        qk.setYearCostRgfZb(NumberUtil.divideUseProportion(yearCostRgfJe,2,basePrice));
        qk.setYearCostClfZb(NumberUtil.divideUseProportion(yearCostClfJe,2,basePrice));
        qk.setYearCostJxfZb(NumberUtil.divideUseProportion(yearCostJxfJe,2,basePrice));
        qk.setYearCostQtzjfZb(NumberUtil.divideUseProportion(yearCostQtzjfJe,2,basePrice));
        qk.setYearCostJjfZb(NumberUtil.divideUseProportion(yearCostJjfJe,2,basePrice));
        qk.setYearCostJjfGlrygzZb(NumberUtil.divideUseProportion(yearCostJjfGlrygzJe,2,basePrice));
        qk.setYearCostFbgczcZb(NumberUtil.divideUseProportion(yearCostFbgczcJe,2,basePrice));
        qk.setYearCostFyglZb(NumberUtil.divideUseProportion(yearCostFyglJe,2,basePrice));
        qk.setYearCostCwfyZb(NumberUtil.divideUseProportion(yearCostCwfyJe,2,basePrice));
        qk.setYearCostQtZb(NumberUtil.divideUseProportion(yearCostQtJe,2,basePrice));

        BigDecimal hj = NumberUtil.adds(yearCostRgfJe,
                yearCostClfJe,
                yearCostJxfJe,
                yearCostQtzjfJe,
                yearCostJjfJe,
                yearCostJjfGlrygzJe,
                yearCostFbgczcJe,
                yearCostFyglJe,
                yearCostCwfyJe,
                yearCostQtJe);
        // 成本合计 金额
        qk.setYearCostHjJe(hj);
        // 成本合计  其中：专业分包 = 分包工程支出的金额
        qk.setYearCostHjZb(yearCostFbgczcJe);
        // 甲供材 = 本年完成产值情况 - 甲供材
        qk.setYearJgc(NumberUtil.toCurrency(yearCzJgc));


        // 产值 - 成本 = 利润额
        BigDecimal lre = NumberUtil.subtract(yearCzPdHj, hj);
        qk.setLre(lre);
        qk.setLrl(NumberUtil.divideUseProportion(lre,2,basePrice));
    }

    private void zyfz(List<DataAnalysisNdgccbqkhzb> resList) {
        Map<String, List<DataAnalysisNdgccbqkhzb>> zyGroup = resList.stream().filter(e -> StrUtil.isNotBlank(e.getZy())).collect(Collectors.groupingBy(DataAnalysisNdgccbqkhzb::getZy));
        Set<Map.Entry<String, List<DataAnalysisNdgccbqkhzb>>> entries = zyGroup.entrySet();
        for (Map.Entry<String, List<DataAnalysisNdgccbqkhzb>> entry : entries) {
            String key = entry.getKey();
            List<DataAnalysisNdgccbqkhzb> value = entry.getValue();
            DataAnalysisNdgccbqkhzb zj = zj(value);
            zj.setProName(key + "-专业小计");
            resList.add(zj);
        }
    }
    private DataAnalysisNdgccbqkhzb zj(List<DataAnalysisNdgccbqkhzb> resList) {
        DataAnalysisNdgccbqkhzb re = new DataAnalysisNdgccbqkhzb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal yearCzPdHj = BigDecimal.ZERO;
        BigDecimal yearCzPdZyfb = BigDecimal.ZERO;
        BigDecimal yearCzSj = BigDecimal.ZERO;
        BigDecimal yearCzJgc = BigDecimal.ZERO;
        BigDecimal yearCostRgfJe = BigDecimal.ZERO;
        BigDecimal yearCostClfJe = BigDecimal.ZERO;
        BigDecimal yearCostJxfJe = BigDecimal.ZERO;
        BigDecimal yearCostQtzjfJe = BigDecimal.ZERO;
        BigDecimal yearCostJjfJe = BigDecimal.ZERO;
        BigDecimal yearCostJjfGlrygzJe = BigDecimal.ZERO;
        BigDecimal yearCostFbgczcJe = BigDecimal.ZERO;
        BigDecimal yearCostFyglJe = BigDecimal.ZERO;
        BigDecimal yearCostCwfyJe = BigDecimal.ZERO;
        BigDecimal yearCostQtJe = BigDecimal.ZERO;
        BigDecimal yearCostHjJe = BigDecimal.ZERO;

        for (DataAnalysisNdgccbqkhzb qk : resList) {
            htTotal =               NumberUtil.add(qk.getHtTotal(),htTotal);
            jzmj =                  NumberUtil.add(qk.getJzmj(),jzmj);
            yearCzPdHj =            NumberUtil.add(qk.getYearCzPdHj(),yearCzPdHj);
            yearCzPdZyfb =          NumberUtil.add(qk.getYearCzPdZyfb(),yearCzPdZyfb);
            yearCzSj =              NumberUtil.add(qk.getYearCzSj(),yearCzSj);
            yearCzJgc =             NumberUtil.add(qk.getYearCzJgc(),yearCzJgc);
            yearCostRgfJe =         NumberUtil.add(qk.getYearCostRgfJe(),yearCostRgfJe);
            yearCostClfJe =         NumberUtil.add(qk.getYearCostClfJe(),yearCostClfJe);
            yearCostJxfJe =         NumberUtil.add(qk.getYearCostJxfJe(),yearCostJxfJe);
            yearCostQtzjfJe =       NumberUtil.add(qk.getYearCostQtzjfJe(),yearCostQtzjfJe);
            yearCostJjfJe =         NumberUtil.add(qk.getYearCostJjfJe(),yearCostJjfJe);
            yearCostJjfGlrygzJe =   NumberUtil.add(qk.getYearCostJjfGlrygzJe(),yearCostJjfGlrygzJe);
            yearCostFbgczcJe =      NumberUtil.add(qk.getYearCostFbgczcJe(),yearCostFbgczcJe);
            yearCostFyglJe =        NumberUtil.add(qk.getYearCostFyglJe(),yearCostFyglJe);
            yearCostCwfyJe =        NumberUtil.add(qk.getYearCostCwfyJe(),yearCostCwfyJe);
            yearCostQtJe =          NumberUtil.add(qk.getYearCostQtJe(),yearCostQtJe);
            yearCostHjJe =          NumberUtil.add(qk.getYearCostHjJe(),yearCostHjJe);
        }

        re.setHtTotal(htTotal);
        re.setJzmj(jzmj);
        re.setYearCzPdHj(yearCzPdHj);
        re.setYearCzPdZyfb(yearCzPdZyfb);
        re.setYearCzSj(yearCzSj);
        re.setYearCzJgc(yearCzJgc);
        re.setYearCostRgfJe(yearCostRgfJe);
        re.setYearCostClfJe(yearCostClfJe);
        re.setYearCostJxfJe(yearCostJxfJe);
        re.setYearCostQtzjfJe(yearCostQtzjfJe);
        re.setYearCostJjfJe(yearCostJjfJe);
        re.setYearCostJjfGlrygzJe(yearCostJjfGlrygzJe);
        re.setYearCostFbgczcJe(yearCostFbgczcJe);
        re.setYearCostFyglJe(yearCostFyglJe);
        re.setYearCostCwfyJe(yearCostCwfyJe);
        re.setYearCostQtJe(yearCostQtJe);
        re.setYearCostHjJe(yearCostHjJe);
        re.hb();
        calcZb(re);
        return re;
    }


}
